import cv2
import numpy as np
import matplotlib.pyplot as plt

dst = cv2.imread('7/001.jpg', 0)
# dst = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY)
dst = cv2.resize(dst, (640, 480))

def binary(dst):
    _, thresh1 = cv2.threshold(dst, 140, 255, cv2.THRESH_BINARY)
    # cv2.imshow('ori', dst)
    cv2.imshow('thresh1', thresh1)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    # plt.imshow(thresh1, cmap='gray')
    # plt.show()

def unbinary(dst):
    _, thresh2 = cv2.threshold(dst, 50, 255, cv2.THRESH_BINARY_INV)
    cv2.imshow('thresh2', thresh2)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

def binary_down_zero(dst):
    _, thresh3 = cv2.threshold(dst, 150, 255, cv2.THRESH_TOZERO)
    cv2.imshow('thresh3', thresh3)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

def binary_up_zero(dst):
    _, thresh4 = cv2.threshold(dst, 150, 255, cv2.THRESH_TOZERO_INV)
    cv2.imshow('thresh4', thresh4)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

def trunc(dst):
    _, thresh5 = cv2.threshold(dst, 150, 255, cv2.THRESH_TRUNC)
    cv2.imshow('thresh5', thresh5)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


def adaptive(dst):
    # dst = cv2.cvtColor(dst, cv2.COLOR_BAYER_BG2GRAY)
    thresh1 =cv2.adaptiveThreshold(dst,50,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,25,10)
    thresh2 = cv2.adaptiveThreshold(dst,50,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,25,10)
    images = [dst,thresh1,thresh2]

    titles = ['Original', 'Gaussian Adaptive', 'Mean Adaptive']

    for i in range(3):
        plt.subplot(1,3, i+1)
        plt.imshow(images[i], cmap='gray')
        plt.title(titles[i])
        plt.axis('off')

    plt.show()


def main(dst):
    binary(dst)
    unbinary(dst)
    binary_down_zero(dst)
    binary_up_zero(dst)
    trunc(dst)
    adaptive(dst)

if __name__ == '__main__':
    main(dst)